/**
 *  device.h
 *
 *  Copyright (C) 2008-2009 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __DEVICE_H__
#define __DEVICE_H__

#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/list.h"
#include "include/mem.h"
#include "include/irq.h"
#include "common/string.h"
#endif


#define DEVICE_NR 255  // Suport to 255 devices.


typedef struct _device {
    list_t link;
    word_t used_cnt;
    char_t *name;
    char_t mid;
    char_t sid;
    char_t dev_type;
    bool_t delete_flag;
    void (*release)(void);
} device_t;

// For dev_type
#define CHDEV 0x01  // Char device.
#define BLKDEV 0x02 // Block device.
#define NETDEV 0x03 // network device.

#define DEV_FALSE FALSE
#define DEV_OK TRUE
#define DEV_NO_MEM 0x2
#define DEV_NO_DEVICE 0x3
#define DEV_BUSY 0x4
#define DEV_FREE 0x5

typedef void (*device_init_func)(void);

/* Initialize all devices. */
void device_init(void);

/* Char device register. */
word_t char_device_register(device_t *dev,         /* Pointer to device node */
                            const char_t *Name,    /* Device name */
                            char_t Mid,            /* Main ID */
                            char_t Sid,            /* Secondary ID */
                            void (*release)(void));/* Callback function */

void char_device_unregister(uword_t chdevMID, /* Main ID */
                            uword_t chdevSID);/* Secondary ID */


/* Open a device */
device_t *device_open(const char_t *name);

/* Close a device */
void device_close(device_t *dev);



#endif

